<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户名称" align="center" prop="shopName" />
      <el-table-column label="门店地址" align="center" prop="address" />
      <el-table-column label="负责人" align="center" prop="contactName" />
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改商户管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="merchantRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="uId">
          <el-input v-model="form.uId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="店名" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店名" />
        </el-form-item>
        <el-form-item label="营业执照号" prop="licenseNum">
          <el-input v-model="form.licenseNum" placeholder="请输入营业执照号" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="营业时间" prop="hours">
          <el-input v-model="form.hours" placeholder="请输入营业时间" />
        </el-form-item>
        <el-form-item label="注册时间" prop="regTime">
          <el-date-picker clearable
                          v-model="form.regTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证正反面照片" prop="idCardPhotos">
          <image-upload v-model="form.idCardPhotos"/>
        </el-form-item>
        <el-form-item label="营业执照照片" prop="licensePhoto">
          <image-upload v-model="form.licensePhoto"/>
        </el-form-item>
        <el-form-item label="门面照片" prop="storePhoto">
          <image-upload v-model="form.storePhoto"/>
        </el-form-item>
        <el-form-item label="商家介绍" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核状态：0-待审核，1-已通过，2-已拒绝" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
                v-for="dict in merchant_audit_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="逻辑删除：1-已删除，0-未删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入逻辑删除：1-已删除，0-未删除" />
        </el-form-item>
        <el-form-item label="0暂停营业、1开始营业" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in merchant_open_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Merchant">
import { listMerchant, getMerchant, delMerchant, addMerchant, updateMerchant } from "@/api/merchant/merchant"
import {parseTime} from "../../../utils/ruoyi.js";

const { proxy } = getCurrentInstance()
const { merchant_open_status, merchant_audit_status } = proxy.useDict('merchant_open_status', 'merchant_audit_status')

const merchantList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    uId: null,
    contactName: null,
    idCard: null,
    phone: null,
    shopName: null,
    licenseNum: null,
    provinceId: null,
    cityId: null,
    districtId: null,
    address: null,
    hours: null,
    regTime: null,
    idCardPhotos: null,
    licensePhoto: null,
    storePhoto: null,
    intro: null,
    auditStatus: null,
    isDeleted: null,
    status: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商户管理列表 */
function getList() {
  loading.value = true
  listMerchant(queryParams.value).then(response => {
    merchantList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    mId: null,
    uId: null,
    contactName: null,
    idCard: null,
    phone: null,
    shopName: null,
    licenseNum: null,
    provinceId: null,
    cityId: null,
    districtId: null,
    address: null,
    hours: null,
    regTime: null,
    idCardPhotos: null,
    licensePhoto: null,
    storePhoto: null,
    intro: null,
    auditStatus: null,
    isDeleted: null,
    status: null
  }
  proxy.resetForm("merchantRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.mId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商户管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _mId = row.mId || ids.value
  getMerchant(_mId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商户管理"
    console.log("regTime", form.value.regTime)
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantRef"].validate(valid => {
    if (valid) {
      if (form.value.mId != null) {
        updateMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _mIds = row.mId || ids.value
  proxy.$modal.confirm('是否确认删除商户管理编号为"' + _mIds + '"的数据项？').then(function() {
    return delMerchant(_mIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('merchant/merchant/export', {
    ...queryParams.value
  }, `merchant_${new Date().getTime()}.xlsx`)
}

getList()
</script>